(function(jq){
	var flag= "";
	Array.prototype.indexOf = function(val) {               
	for (var i = 0; i < this.length; i++) {   
	    if (this[i] == val) return i;   
	}   
	return -1;   
	}; 
	Array.prototype.remove = function(val) {   
		var index = this.indexOf(val);   
		if (index > -1) {   
			this.splice(index, 1);   
		}
		return this
	};
  jq.fn.jqTreeTable=function(map, options){
    var opts = jq.extend({openImg:"",shutImg:"",leafImg:"",lastOpenImg:"",lastShutImg:"",lastLeafImg:"",vertLineImg:"",blankImg:"",collapse:false,column:0,striped:false,highlight:false,state:true},options),
    mapa=[],mapb=[],tid=this.attr("id"),collarr=[],
	  stripe=function(){
      if(opts.striped){
  		  $("#"+tid+" tr:visible").filter(":even").addClass("even").end().filter(":odd").removeClass("even");
      }
	  },
    buildText = function(parno, preStr){
      var mp=mapa[parno], ro=0, pre="", pref, img;
      for (var y=0,yl=mp.length;y<yl;y++){
        ro = mp[y];
        if (mapa[ro]){
          pre=(y==yl-1)? opts.blankImg: opts.vertLineImg;
          img=(y==yl-1)? opts.lastOpenImg: opts.openImg;
          mapb[ro-1] = preStr + '<img src="'+img+'" class="parimg" id="'+tid+ro+'">';
          pref = preStr + '<img src="'+pre+'" class="preimg">';
          arguments.callee(ro, pref);//回调函数
        }else{
          img = (y==yl-1)? opts.lastLeafImg: opts.leafImg;
          mapb[ro-1] = preStr + '<img src="'+img+'" class="ttimage" id="'+tid+ro+'">';
        }
      }
    },
    expandKids = function(num, last){
    	if(mapa[num]){
  		  jq("#"+tid+num).attr("src", (last)? opts.lastOpenImg: opts.openImg);//打开图片选择器
  		  for (var x=0, xl=mapa[num].length;x<xl;x++){
  			var mnx = mapa[num][x];
  			jq("#"+tid+mnx).parents("tr").removeClass("collapsed");
  				if (mapa[mnx] && jq.inArray(mnx, collarr)<0){
  					if(jq.inArray(mnx+"", jq.unique(flag.split(",").remove("")))>=0 ){
  						jq("#"+tid+num+1).attr("src", (last)? opts.lastShutImg: opts.shutImg);
  						while(jq.inArray(mnx+"", jq.unique(flag.split(",").remove("")))>=0 || mnx == mapa[mnx]-1){
  							mnx=mnx+1;
  						}
  						arguments.callee(mnx,(x==xl-1));
  					}

  					else{
  						 arguments.callee(mnx,(x==xl-1));	
  					}
  					 
  			}
  		  }
  	  }else{
  		jq("#"+tid+mnx).attr("src",opts.lastLeafImg);
  	  }
  		flag = flag.split(",").remove(num).toString();
    },
    collapseKids = function(num, last){
      jq("#"+tid+num).attr("src", (last)? opts.lastShutImg: opts.shutImg);//折叠图片选择器
      for (var x=0, xl=mapa[num].length;x<xl;x++){
        var mnx = mapa[num][x];
        jq("#"+tid+mnx).parents("tr").addClass("collapsed");
        if (mapa[mnx]){
          arguments.callee(mnx,(x==xl-1));
        }
      }
    },
  	creset = function(num, exp){
  		var o = (exp)? collarr.splice(jq.inArray(num, collarr), 1): collarr.push(num);
      cset(tid,collarr);
  	},
  	cget = function(n){
	  	var v='',c=' '+document.cookie+';',s=c.indexOf(' '+n+'=');
	    if (s>=0) {
	    	s+=n.length+2;
	      v=(c.substring(s,c.indexOf(';',s))).split("|");
	    }
	    return v||0;
  	},
    cset = function (n,v) {
  		jq.unique(v);//去重函数
	  	document.cookie = n+"="+v.join("|")+";";
	  };
    for (var x=0,xl=map.length; x<xl;x++){
      num = map[x];
      if (!mapa[num]){
        mapa[num]=[];
      }
      mapa[num].push(x+1);
    }
    buildText(0,"");
    jq("tr", this).each(function(i){
      jq(this).children("td").eq(opts.column).prepend(mapb[i]);
      var localObj = window.location;
      var contextPath = localObj.pathname.split("/")[1];
      var basePath = localObj.protocol+"//"+localObj.host+"/"+contextPath;
      var oraID = jq(this).children("td").eq(1).find("span").attr("id");

      jq(this).children("td").eq(2).prepend("<img height='30Px' src='"+basePath+"/common/images/TreeTable/u116.png' onClick='addOrg("+oraID+");'><img height='30Px' src='"+basePath+"/common/images/TreeTable/u108.png' onClick='modifyOrg(\""+oraID+"\");'><img height='30Px' src='"+basePath+"/common/images/TreeTable/u50.png' onClick='deleteOrg(\""+oraID+"\");'>");//操作按钮放置处
    });
		collarr = cget(tid)||opts.collapse||collarr;
		if (collarr.length){
			cset(tid,collarr);
	    for (var y=0,yl=collarr.length;y<yl;y++){
	      collapseKids(collarr[y],($("#"+collarr[y]+ " .parimg").attr("src")==opts.lastOpenImg));
	    }
		}
    stripe();
    jq(".parimg", this).each(function(i){
      var jqt = jq(this),last;
      jqt.click(function(){//点击事件
        var num = parseInt(jqt.attr("id").substr(tid.length));
        if (jqt.parents("tr").next().is(".collapsed")){
          expandKids(num, (jqt.attr("src")==opts.lastShutImg));
          if(opts.state){creset(num,true);}
        }else{
        	flag += num+",";
          collapseKids(num, (jqt.attr("src")==opts.lastOpenImg));
          if(opts.state){creset(num,false);}
        }
        stripe();
      });
    });
    if (opts.highlight){
      jq("tr", this).hover(
        function(){jq(this).addClass("over");},
        function(){jq(this).removeClass("over");}
      );
    };
  };
  return this;
})(jQuery);
